package com.llb666.e.shop.infrastructure.persistence;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llb666.e.shop.api.vo.MyOrdersVo;
import com.llb666.e.shop.api.vo.OrderStatusCountsVo;
import com.llb666.e.shop.domain.entity.OrderStatus;
import com.llb666.e.shop.domain.entity.Orders;
import com.llb666.e.shop.domain.repository.OrdersRepository;
import com.llb666.e.shop.infracore.enums.OrderStatusEnum;
import com.llb666.e.shop.infracore.enums.YesOrNo;
import com.llb666.e.shop.infracore.web.PagedGridResult;
import com.llb666.e.shop.infrastructure.convert.OrderCvt;
import com.llb666.e.shop.infrastructure.mapper.OrderStatusMapper;
import com.llb666.e.shop.infrastructure.mapper.OrdersMapper;
import com.llb666.e.shop.infrastructure.po.OrderStatusPo;
import com.llb666.e.shop.infrastructure.po.OrdersPo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 斌~
 * @date 2022/9/12 下午7:09
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class OrdersRepositoryImpl implements OrdersRepository {

    private final OrdersMapper ordersMapper;
    private final OrderStatusMapper orderStatusMapper;

    @Override
    public OrderStatusCountsVo getOrderStatusCounts(String userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        // 待付款
        map.put("orderStatus", OrderStatusEnum.WAIT_PAY.type);
        int waitPayCounts = ordersMapper.getMyOrderStatusCounts(map);
        // 待发货
        map.put("orderStatus", OrderStatusEnum.WAIT_DELIVER.type);
        int waitDeliverCounts = ordersMapper.getMyOrderStatusCounts(map);
        // 待收货
        map.put("orderStatus", OrderStatusEnum.WAIT_RECEIVE.type);
        int waitReceiveCounts = ordersMapper.getMyOrderStatusCounts(map);
        // 交易成功
        map.put("orderStatus", OrderStatusEnum.SUCCESS.type);
        map.put("isComment", YesOrNo.NO.type);
        int waitCommentCounts = ordersMapper.getMyOrderStatusCounts(map);
        return new OrderStatusCountsVo(waitPayCounts, waitDeliverCounts, waitReceiveCounts, waitCommentCounts);

    }

    @Override
    public void updateDeliverOrderStatus(String orderId) {
        OrderStatusPo orderStatus = new OrderStatusPo();
        orderStatus.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE.type);
        orderStatus.setDeliverTime(new Date());

        Example example = new Example(OrderStatus.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId", orderId);
        criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_RECEIVE.type);
        orderStatusMapper.updateByExampleSelective(orderStatus, example);
    }

    @Override
    public boolean updateReceiveOrderStatus(String orderId) {
        OrderStatusPo updateOrder = new OrderStatusPo();
        updateOrder.setOrderStatus(OrderStatusEnum.SUCCESS.type);
        updateOrder.setSuccessTime(new Date());

        Example example = new Example(OrderStatus.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId", orderId);
        criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_RECEIVE.type);

        int result = orderStatusMapper.updateByExampleSelective(updateOrder, example);
        return result == 1;
    }

    @Override
    public boolean deleteOrder(String userId, String orderId) {
        OrdersPo delOrder = new OrdersPo();
        delOrder.setIsDelete(YesOrNo.YES.type);
        delOrder.setUpdatedTime(new Date());

        Example example = new Example(Orders.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", orderId);
        criteria.andEqualTo("userId", userId);

        int result = ordersMapper.updateByExampleSelective(delOrder, example);
        return result == 1;
    }

    @Override
    public Orders queryMyOrder(String userId, String orderId) {
        OrdersPo orders = new OrdersPo();
        orders.setUserId(userId);
        orders.setId(orderId);
        orders.setIsComment(YesOrNo.NO.type);
        return OrderCvt.INSTANCE.po2do(ordersMapper.selectOne(orders));
    }

    @Override
    public void updateCommentsStatus(String orderId) {
        OrdersPo order = new OrdersPo();
        order.setId(orderId);
        order.setIsComment(YesOrNo.YES.type);
        ordersMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void insert(Orders order) {
        ordersMapper.insert(OrderCvt.INSTANCE.do2po(order));
    }

    @Override
    public PagedGridResult<MyOrdersVo> queryMyOrderByStatus(String userId, Integer orderStatus,Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<MyOrdersVo> orderByUserIdAndStatus = ordersMapper.getOrderByUserIdAndStatus(userId, orderStatus);
        PageInfo<MyOrdersVo> myOrdersVoPageInfo = new PageInfo<>(orderByUserIdAndStatus);
        return PagedGridResult.setterPagedGrid(myOrdersVoPageInfo);
    }

    @Override
    public PagedGridResult<OrderStatus> getMyOrderTrend(String userId,Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<OrderStatus> myOrderTrend = ordersMapper.getMyOrderTrend(userId);
        PageInfo<OrderStatus> orderStatusPageInfo = new PageInfo<>(myOrderTrend);
        return PagedGridResult.setterPagedGrid(orderStatusPageInfo);
    }
}
